define(["Zepto", 
        "underscore", 
        "Backbone", 
        "Handlebars",
        "models/studentevotacorso",
        "text!templates/vote/vote.html",
        "views/common/menuview"
], function($, _, Backbone, Handlebars, Studentevotacorso, template, MenuView) {
	
   
	  var VoteView = Backbone.View.extend({

	        template: Handlebars.compile(template),

	        render: function(eventName) {
	        	this.$el.html(this.template);
				
	        	// menu top
	        	this.menutop = new MenuView({
	               	el : $('#menu', this.el),
	    			model : this.model
				});
	            this.menutop.render();
	        	
	        	
	            return this;
	        },

	        postRender: function(eventName) {
	            var scrollableframe = new iScroll('scrollableframe',
	                          { useTransition:false,
	                            checkDOMChanges: true,
	                            hideScrollbar: true,
	                            fixedScrollbar:true,
	                            bounceLock: true,
	                            onBeforeScrollStart: function (e) {
	                                var target = e.target;
	                                while (target.nodeType != 1) target = target.parentNode;

	                                if (target.tagName != 'SELECT' && target.tagName != 'OPTION' && target.tagName != 'INPUT' && target.tagName != 'TEXTAREA' && target.tagName != 'BUTTON')
	                                    e.preventDefault();
	                            } }
	                        );
	        },


	        events: {
	        	'click #votebutton': 'insertvote',
	            'touchstart .button': 'over',
	            'touchend .button': 'left',
	            'touchcancel .button': 'left',
	            'touchstart button': 'over',
	            'touchend button': 'left',
	            'touchcancel button': 'left'
	        },
	        
	        insertvote: function(event) {
	        	var materiale = $("#mater input[type='radio']:checked").val();
	        	var interesse = $("#inte input[type='radio']:checked").val();
	        	var utilita = $("#util input[type='radio']:checked").val();
	        	var facilita = $("#fac input[type='radio']:checked").val();
	        	var docente = $("#doc input[type='radio']:checked").val();
	        	//alert('docente ' +  parseInt(docente));
	        	var votacorso = new Studentevotacorso();
	        	votacorso.save({
	        		corso: {"__type":"Pointer","className":"corso","objectId":this.model.get('corso').objectId},
	        		materiale: parseInt(materiale),
	        		interesse:  parseInt(interesse),
	        		utilita:  parseInt(utilita),
	        		facilita:  parseInt(facilita),
	        		docente:  parseInt(docente)
	        	},{
	            	success: function(){
	            		alert('Hai votato!');
	            	}
	            });
	        },

	        over: function(event) {
	            $(event.currentTarget).addClass('fake-active');
	        },

	        left: function(event) {
	            $(event.currentTarget).removeClass('fake-active');
	        }
	    });

    return VoteView;

});

